package com.ciwong.epaper.modules.msg.dao;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.ciwong.epaper.modules.epaper.bean.BasePaging;
import com.ciwong.epaper.modules.epaper.dao.HeadOverrideRequest;
import com.ciwong.epaper.modules.me.bean.MyWork;
import com.ciwong.epaper.modules.me.bean.WorkContents;
import com.ciwong.epaper.modules.msg.bean.IsDelWorkBean;
import com.ciwong.epaper.modules.msg.bean.MessageCount;
import com.ciwong.epaper.modules.msg.bean.MessageInfo;
import com.ciwong.epaper.modules.msg.bean.PushInfo;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.BaseRequestUtil;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Message 相关网络请求
 * Created by res-yinxue on 2016/6/20.
 */
public class MsgRequest {
    /**
     * 获取未读消息
     */
    public static void getNoReadMsgCount(int brandId, long userId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_NOREAD_MSGCOUNT);
        params.put("brandId", brandId + "");
        params.put("userId", userId + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<List<MessageCount>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    /**
     * 获取消息列表
     */
    public static void getMessageList(int brandId, long userId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG);
        params.put("brandId", brandId + "");
        params.put("userId", userId + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<List<MessageInfo>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    public static void isDelWork(String workId, int brandId, long userId, int senderId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_ISDELWORK);
        params.put("senderId", senderId + "");
        params.put("userId", userId + "");
        params.put("brandId", brandId + "");
        params.put("workId", workId + "");
        HttpRequest httpRequest = new HttpRequest(params, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                callBack.failed(volleyError);
            }
        }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(String responseStr) {
                callBack.success(responseStr);
            }

            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                callBack.success(obj);
            }

            @Override
            public void success(BaseRequest.XmlHandler xmlHandler) {
                callBack.success(xmlHandler);
            }

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }
        });

        httpRequest.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<IsDelWorkBean>() {
        }.getType();
        httpRequest.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(httpRequest);
        callBack.setUrl(httpRequest.getUrl());


    }

    /**
     * 获取分组下的消息列表
     */
    public static void getMsgsBySenderId(int brandId, long userId, int msgGroupType,
                                         int page, int pageSize, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG_BY_SENDERID);
        params.put("page", page + "");
        params.put("pageSize", pageSize + "");
        params.put("msgGroup", msgGroupType + "");
         params.put("userId", userId + "");
        params.put("brandId", brandId + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                        if (volleyError != null && volleyError.getMessage() != null) {
                            CWLog.e("TAT", "volleyError=" + volleyError.getMessage().toString());
                        }
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                    CWLog.e("TAT", "obj=" + obj);
                } else {
                    callBack.failed(errorCode, msg);
                    CWLog.e("TAT", "error-else-msg=" + msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
                CWLog.e("TAT", "error msg=" + msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<BasePaging<MessageInfo>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    /**
     * 向服务器发送“推送ID”
     */
    public static void setUserPushId(int brandId, long userId, String tagno, int ptypeId,
                                     String pushId, final BaseCallBack callBack) {

        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG_PUSHID);
        params.put("ptypeId", ptypeId + "");
        params.put("pushId", pushId);
        params.put("userId", userId + "");
        params.put("brandId", brandId + "");
        params.put("tagno", tagno);
        //Request.Method.POST post请求
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                    CWLog.e("TAT", "【setPushId】 success!");
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
                CWLog.e("TAT", "【setPushId】 error msg=" + msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(PushInfo.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    private static void setMsgStateAllRead(String msgType, String brandId, final BaseCallBack baseCallBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG_SETALLREAD);
        params.put("msgType", msgType + "");
        params.put("brandId", brandId + "");
        HttpRequest httpRequest = new HttpRequest(params, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
//                System.out.println("setMsgStateAllReadobj:"+obj+",msg:"+msg);
//                setMsgStateAllReadobj:false,msg:success
                baseCallBack.success(obj);
            }

            @Override
            public void error(int errorCode, String msg) {
//                System.out.println("setMsgStateAllReaderrorCode:"+errorCode+",msg:"+msg);
                baseCallBack.failed(errorCode, msg);
            }
        });
        httpRequest.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<Boolean>() {
        }.getType();
        httpRequest.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(httpRequest);
        baseCallBack.setUrl(httpRequest.getUrl());

    }

    /**
     * 上传未读信息的ID
     */
    public static void setMsgStateRead(long userId, String msgIds, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG_SETREAD);
        params.put("msgIds", msgIds);
        params.put("userId", userId + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    /**
     * 根据作业ID获取作业详情
     */

    public static void getUserWorkDetail(String workId, long userId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG_WORKDETAIL);
        params.put("workId", workId);
        params.put("userId", userId + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        /*Type responseType = new TypeToken<List<MyWork>>() {
        }.getType();
        request.setResponseType(responseType);*/
        request.setResponseClazz(MyWork.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }


    /**
     * 根据doWorkId获取作业内容
     *
     * @param doWorkId
     * @param callBack
     */
    public static void getDoWorkInfoFromNet(String doWorkId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_DO_WORK_INFO);
        params.put("doWorkId", doWorkId);
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                        CWLog.e("TAT", "volleyError.toString()");
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                super.success(obj, resultCode, errorCode, msg);
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }

            @Override
            public void error(int errorCode, String msg) {
                super.error(errorCode, msg);
                callBack.failed(errorCode, msg);
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(WorkContents.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 新的获取未读消息数量的几口
     * @param json
     * @param callBack
     */
    public static void getNoReadMsgCountNew(final String json, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(com.ciwong.libs.utils.volley.HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_NOREAD_MSGCOUNT_NEW);
        HeadOverrideRequest request = new HeadOverrideRequest(Request.Method.POST, params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    if (null != callBack)
                        callBack.success(obj);
                } else {
                    if (null != callBack)
                        callBack.failed(errorCode, msg);
                }
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return json.getBytes();
            }
        };
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<List<MessageInfo>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 获取消息列表
     */
    public static void getMessageListNew(final String json, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(com.ciwong.libs.utils.volley.HttpRequest.REQUEST_MODULES, MsgAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, MsgAction.ACTION_GET_MSG_NEW);
        HeadOverrideRequest request = new HeadOverrideRequest(Request.Method.POST, params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    if (null != callBack)
                        callBack.success(obj);
                } else {
                    if (null != callBack)
                        callBack.failed(errorCode, msg);
                }
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return json.getBytes();
            }
        };
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<List<MessageInfo>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

}
